const App = {
  mixins: [Query],
  data() {
    return {
      page: {
        value1: "",
        PageNumber: 1,
        RowAmount: 10,
        Condition: window.btoa(unescape(encodeURIComponent("%%"))),
      }
    };
  },
  mounted() {
  },
  methods: {
    // 添加处理下属组织的方法
    handleSubOrganization(row) {
      window.location.href = "./memberAdd.html?action=Modify&Organization_SerialNumber=" + row.Organization_SerialNumber;
    },
    validateAllForms() {
      return new Promise((resolve) => {
        this.$refs.form.validate(valid => resolve(valid))
      });
    },

    //!Object.keys(obj).some(item => item.includes('_Original'))
    //     postgetList() {
    //   const _this = this;
    //   let strWhere = ""

    //   $.ajax({
    //     headers: requestHeaders(),
    //     url: `${mh}/generalBusiness/generalQueryForObtainingTrees`,
    //     type: "GET",
    //     data: {
    //       TableName: "north_organization",
    //       //Condition: strWhere //暂时不支持加条件?
    //     },
    //     success: (data) => {
    //       console.log(data);
    //       _this.Tabletdata = data; // 直接使用 this

    //     },
    //     complete: (XMLHttpRequest) => {
    //       requestPretreatment(XMLHttpRequest);
    //     },
    //     dataType: "json",
    //   });
    // },

    postgetList() {
      const _this = this;
      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/generalBusiness/generalQueryForObtainingTrees`,
        type: "GET",
        data: {
          TableName: "north_organization",
        },
        success: (data) => {

          console.log(data);
          _this.Tabletdata=data;
          // 获取所有下属组织关系
          // $.ajax({
          //   headers: requestHeaders(),
          //   url: `${mh}/generalBusiness/generalQueryByTableName`,
          //   type: "POST",
          //   data: {
          //     Data: "north_organization_childrenorg",
          //     Condition: "Row_Status='0'",
          //   },
          //   success: (relationData) => {
          //     // 创建父组织ID到下属组织数量的映射
          //     const subOrgCountMap = {};
          //     relationData.forEach(relation => {
          //       const parentId = relation.Parent_Organization_SerialNumber;
          //       subOrgCountMap[parentId] = (subOrgCountMap[parentId] || 0) + 1;
          //     });

          //     // 为每个组织节点添加subOrgCount属性
          //     const addSubOrgCount = (nodes) => {
          //       return nodes.map(node => {
          //         const count = subOrgCountMap[node.Organization_SerialNumber] || 0;
          //         return {
          //           ...node,
          //           subOrgCount: count,
          //           children: node.children ? addSubOrgCount(node.children) : []
          //         };
          //       });
          //     };

          //     _this.Tabletdata = addSubOrgCount(data);
          //   },
          //   error: (xhr) => console.error("下属组织关系加载失败", xhr)
          // });
        },
        error: (xhr) => console.error("组织树加载失败", xhr)
      });
    },

    // 添加组织
    addOrganization() {
      window.location.href = "./add.html?action=Add";
    },

    // 编辑组织
    handleEdit(row) {
      window.location.href = "./add.html?action=Modify&nodeKey=" + row.Organization_SerialNumber;
    },

    // 查询
    check() {
      this.page.PageNumber = 1;
      this.postgetList();
    },

    // 分页大小变化
    handleSizeChange(newSize) {
      this.page.RowAmount = newSize;
      this.postgetList();
    },

    // 当前页变化
    handleCurrentChange(newPage) {
      this.page.PageNumber = newPage;
      this.postgetList();
    }
  }
};

const { createApp } = Vue;
const app = createApp(App);

// 注册图标组件
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}

app.use(ElementPlus, { locale: zhCn });
app.mount("#app");